/**
 * @filename:CarServiceImpl 2019-01-17 14:56:43
 * @project skz  V1.3.3
 * Copyright(c) 2018 Liu,Wenzhao Co. Ltd. 
 * All right reserved. 
 */
package com.bingsum.service;

import com.bingsum.annotation.Api;
import com.bingsum.model.Driver;
import com.bingsum.util.ApiUtil;
import com.bingsum.util.ParaData;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;

import java.util.List;

import com.bingsum.model.Car;
import com.bingsum.mapper.CarMapper;
import tk.mybatis.mapper.entity.Example;

/**   
 *  
 * @Description:  [车辆信息]——SERVICE
 * @Author:       Liu,Wenzhao   
 * @CreateDate:   2019-01-17 14:56:43
 * @Version:      V1.3.3
 *    
 */
@Service("carService")
@Transactional(readOnly = true)
public class CarService{

    @Autowired
    private CarMapper carMapper;

    @Api(notNullPara="currentPage")
    public Object getCarInfoList(ParaData pd) {
        Page<?> page = PageHelper.startPage(pd.getInteger("currentPage"), 20);
        carMapper.getCarInfoList(pd);
        return ApiUtil.returnObject(pd, page);
    }

    @Api
    public Object getCarInfo(ParaData pd) {
        Example example = new Example(Car.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("deleteTime");
        criteria.andEqualTo("id", pd.getString("id"));
        Car car = carMapper.selectOneByExample(example);

        if (car == null){
            return ApiUtil.returnDescFail(pd,"无返回");
        }
        return car;
    }

    @Api
    @Transactional(readOnly = false)
    public Object newCarInfo(ParaData pd) {
        Car car = pd.toBean(Car.class);
        if (car.getId() != null) {
            this.carMapper.updateByPrimaryKeySelective(car);
        } else {
            this.carMapper.insert(car);
        }
        return ApiUtil.returnOK(pd,car);
    }

    @Api
    @Transactional(readOnly = false)
    public Object delCar(ParaData pd) {
        carMapper.delCar(pd);
        Car car = pd.toBean(Car.class);
        return ApiUtil.returnOK(pd,car);
    }
}